//
//  UIViewController+SEGNaviAdd.m
//  SEGWorkOrderTest
//
//  Created by Clavis on 2019/4/2.
//  Copyright © 2019 Clavis. All rights reserved.
//
#import "UIViewController+YYAdd.h"
#import "UIViewController+SEGNaviAdd.h"
#import <objc/runtime.h>
#import "UIControl+YYAdd.h"
#import "NSString+YYAdd.h"

#if __has_include(<Commom/SEGSwizzle.h>)
#import <Commom/SEGSwizzle.h>
#else
#import "SEGSwizzle.h"
#endif

#if __has_include(<Commom/SEGLayoutMacro.h>)
#import <Commom/SEGLayoutMacro.h>
#else
#import "SEGLayoutMacro.h"
#endif

#if __has_include(<Commom/SEGColorManager.h>)
#import <Commom/SEGColorManager.h>
#else
#import "SEGColorManager.h"
#endif

#if __has_include(<Commom/SEGHotAreaButton.h>)
#import <Commom/SEGHotAreaButton.h>
#else
#import "SEGHotAreaButton.h"
#endif

#if __has_include(<SEGPlatform/SEGPlatformManager.h>)
#import <SEGPlatform/SEGPlatformManager.h>
#else
#import "SEGPlatformManager.h"
#endif

#if __has_include(<Commom/SEGUtils.h>)
#import <Commom/SEGUtils.h>
#else
#import "SEGUtils.h"
#endif

// 自定义导航背景视图tag
NSInteger const kNavAdd_customNavTag = 85934;
// 自定义底部商标视图tag
static NSInteger const kBottomBrandTag = 0x123;
// 按钮点击热区域
static CGFloat const kHotArea_W = 44.f;

@implementation UIViewController (SEGNaviAdd)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SEGSwizzleMethod([self class], @selector(viewDidLoad), [self class], @selector(segNaviAdd_viewDidLoad));
    });
}

#pragma mark - Method Swizzling
- (void)segNaviAdd_viewDidLoad {
    if (![NSStringFromClass(self.class) containsString:@"SEG"]) {
        [self segNaviAdd_viewDidLoad];
        return;
    }
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = kColor_bg;
    // 设置返回按钮
    if (self.navigationController.viewControllers.count > 1) {
        UIImage *image = [UIImage imageWithContentsOfFile:SEGPicName(@"back")];
         SEGHotAreaButton *leftButton = [[SEGHotAreaButton alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
        leftButton.minimumAreaSize = CGSizeMake(kHotArea_W, kHotArea_W);
        [leftButton setImage:image forState:UIControlStateNormal];
        [leftButton setImage:image forState:UIControlStateHighlighted];
        [leftButton addTarget:self action:@selector(onNavigationLeftButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
        self.navigationItem.leftBarButtonItem = leftBarButton;
        self.leftBarbuttonWidth = image.size.width;
    }
    // 添加底部角标（导航一级页面不添加）
    if (self.navigationController.viewControllers.count > 1) {
        [self seg_addBottomBrand];
    }
 
    [self segNaviAdd_viewDidLoad];
}

#pragma mark - 导航栏高度
+ (CGFloat)seg_getNavigationBarHeight {
    return 44.f;
}

#pragma mark - 处理底部角标
- (void)seg_removeBottomBrand {
    
}
- (void)seg_addBottomBrand {

}

#pragma mark - 左侧返回按钮
// 导航栏左侧返回按钮 + 标题
- (void)showCustomNavigationBackButton {
}

// 导航栏左侧显示绿色返回按钮
- (void)showCustomNavigationGreedBackButton:(NSString *)imageNorName withPreImage:(NSString *)imagePreName {
    UIImage *image = [UIImage imageWithContentsOfFile:imageNorName];
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    [leftButton setImage:[UIImage imageWithContentsOfFile:imageNorName] forState:UIControlStateNormal];
    [leftButton setImage:[UIImage imageWithContentsOfFile:imagePreName] forState:UIControlStateHighlighted];
    [leftButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [leftButton addTarget:self action:@selector(onNavigationLeftButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.leftBarbuttonWidth = image.size.width;
}

// 显示导航栏左侧的按钮
- (void)showCustomNavigationLeftButtonWithTitle:(NSString *)aTitle image:(UIImage *)aImage hightlightImage:(UIImage *)hImage {
    [self showCustomNavigationLeftButtonWithColorTitle:aTitle image:aImage hightlightImage:hImage titleColor:kColor_W];
}

// 显示导航栏左边的按钮（可设置颜色）
- (void)showCustomNavigationLeftButtonWithColorTitle:(NSString *)aTitle image:(UIImage *)aImage hightlightImage:(UIImage *)hImage titleColor:(UIColor *)color {
    // 导航栏的左边按钮
    UIButton* leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 40)];
    UIImageView* iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, (40 - aImage.size.height) / 2, aImage.size.width, aImage.size.height)];
    [leftButton addSubview:iconImageView];
    if (aTitle) {
        [leftButton setTitle:aTitle forState:UIControlStateNormal];
        [leftButton setTitleColor:color forState:UIControlStateNormal];
        leftButton.titleLabel.font = [UIFont systemFontOfSize:14];
    }
    [iconImageView setImage:aImage];
    if (hImage) [iconImageView setImage:aImage];
    
    [leftButton addTarget:self action:@selector(onNavigationLeftButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.leftBarbuttonWidth = leftButton.frame.size.width;
}

#pragma mark - 右侧按钮
/// 展示导航栏右侧按钮
/// @param rightButton 右侧按钮
- (void)showCustomNavigationRightButton:(UIButton *)rightButton {

}

/// 展示导航栏右侧标题
/// @param rightTitle 右侧标题
- (void)showCustomNavigationRightTitle:(NSString *)rightTitle {
    [self showCustomNavigationRightButtonWithTitle:rightTitle image:nil hightlightImage:nil color:nil];
}

/// 展示导航栏右侧按钮
/// @param rightImage  默认背景图片
/// @param rightHImage 高亮背景图片
- (void)showCustomNavigationRightImage:(UIImage *)rightImage hightlightImage:(UIImage *)rightHImage {
    [self showCustomNavigationRightButtonWithTitle:nil image:rightImage hightlightImage:rightHImage color:nil];
}

// 显示导航栏右边的按钮
- (void)showCustomNavigationRightButtonWithTitle:(NSString *)aTitle image:(UIImage *)aImage hightlightImage:(UIImage *)hImage {
    [self showCustomNavigationRightButtonWithTitle:aTitle image:aImage hightlightImage:hImage color:nil];
}

// 显示导航右侧按钮
- (void)showCustomNavigationRightButtonWithTitle:(NSString *)aTitle image:(UIImage *)aImage hightlightImage:(UIImage *)hImage color:(UIColor *)color {
    // 导航栏的右边按钮
    SEGHotAreaButton *rightButton = [SEGHotAreaButton buttonWithType:UIButtonTypeCustom];
    rightButton.minimumAreaSize = CGSizeMake(kHotArea_W, kHotArea_W);
    rightButton.frame = CGRectMake(0, 0, aImage.size.width, aImage.size.height);
    
    if (aTitle) {
        NSDictionary *attribute = @{ NSFontAttributeName : rightButton.titleLabel.font };
        CGSize textSize = [aTitle boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 0) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
        rightButton.frame = CGRectMake(0, 0, textSize.width, 41);
        
        [rightButton setTitle:aTitle forState:UIControlStateNormal];
        rightButton.titleLabel.font = kFont_13;;
        [rightButton setTitleColor: color ? color : kColor_T forState:UIControlStateNormal];
        [rightButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -13)];
    }
    
    [rightButton setBackgroundImage:aImage forState:UIControlStateNormal];
    if (hImage) {
        [rightButton setBackgroundImage:hImage forState:UIControlStateHighlighted];
    }
    [rightButton addTarget:self action:@selector(onNavigationRightButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    if (aTitle == nil && aImage == nil & hImage == nil) {
        [rightButton removeAllTargets];
    }
}

//显示导航栏右边的按钮 可设置不可用的颜色
- (UIButton *)showCustomNavigationRightButtonWithColorTitle:(NSString *)aTitle titleColor:(UIColor *)color disabledColor:(UIColor *)disabledColor {
    // 导航栏的左边按钮
    SEGHotAreaButton* rightButton = [SEGHotAreaButton buttonWithType:UIButtonTypeCustom];
    rightButton.minimumAreaSize = CGSizeMake(kHotArea_W, kHotArea_W);
    rightButton.frame = CGRectMake(0, 0, 35, 44);
    
    if (aTitle) {
        CGSize size = [aTitle boundingRectWithContentWidth:1000 WithFontOfSize:14];
        rightButton.frame = CGRectMake(0, 0, size.width, 44);
        [rightButton setTitle:aTitle forState:UIControlStateNormal];
        [rightButton setTitleColor:color forState:UIControlStateNormal];
        [rightButton setTitleColor:disabledColor forState:UIControlStateDisabled];
        rightButton.titleLabel.font = kFont_13;
    }
    [rightButton sizeToFit];
    [rightButton addTarget:self action:@selector(onNavigationRightButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
    return rightButton;
}

//显示两个导航栏右边的按钮
- (void)showTwoRightButtonWithTitle:(NSString*)aTitle1 image1:(UIImage*)aImage1 hightlightImage1:(UIImage*)hImage1
                             title2:(NSString*)aTitle2
                             image2:(UIImage*)aImage2
                   hightlightImage1:(UIImage*)hImage2
{
    UIView* buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, aImage2.size.width + aImage1.size.width + 20, aImage2.size.height)];
    UIButton* rightButton1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, aImage1.size.width, aImage1.size.height)];
    UIButton* rightButton2 = [[UIButton alloc] initWithFrame:CGRectMake(aImage1.size.width + 20, 0, aImage2.size.width, aImage2.size.height)];
    [buttonView addSubview:rightButton1];
    [buttonView addSubview:rightButton2];
    if (aTitle1) {
        [rightButton1 setTitle:aTitle1 forState:UIControlStateNormal];
        [rightButton1 setTitleColor:kColor_W forState:UIControlStateNormal];
        rightButton1.titleLabel.font = [UIFont systemFontOfSize:17];
        [rightButton1 setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -13)];
    }
    if (aTitle2) {
        [rightButton2 setTitle:aTitle1 forState:UIControlStateNormal];
        [rightButton2 setTitleColor:kColor_W forState:UIControlStateNormal];
        rightButton2.titleLabel.font = [UIFont systemFontOfSize:17];
        [rightButton2 setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -13)];
    }
    
    [rightButton1 setBackgroundImage:aImage1 forState:UIControlStateNormal];
    [rightButton2 setBackgroundImage:aImage2 forState:UIControlStateNormal];
    
    if (hImage1) {
        [rightButton1 setBackgroundImage:hImage1 forState:UIControlStateHighlighted];
    }
    if (hImage2) {
        [rightButton2 setBackgroundImage:hImage2 forState:UIControlStateHighlighted];
    }
    
    [rightButton1 addTarget:self action:@selector(onNavigationRightButton1Clicked:) forControlEvents:UIControlEventTouchUpInside];
    [rightButton2 addTarget:self action:@selector(onNavigationRightButton2Clicked:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonView];
}

- (void)hlb_showCustomNavigationRightButtonWithColorTitle:(NSString *)aTitle image:(UIImage *)aImage titleColor:(UIColor *)color {
    //导航栏的左边按钮
    SEGHotAreaButton* rightButton = [SEGHotAreaButton buttonWithType:UIButtonTypeCustom];
    rightButton.minimumAreaSize = CGSizeMake(kHotArea_W, kHotArea_W);
    rightButton.backgroundColor = kColor_W;
    CGFloat buttonWidth = 0.f,interval = 3.f;
    if (aTitle) {
        CGFloat titleWidth = [SEGUtils getWidthWithFontSize:16 height:44 text:aTitle];
        buttonWidth += titleWidth;
        rightButton.titleLabel.font = kFont_16;
        color = color ?:kColor_T;
        [rightButton setTitleColor:color forState:UIControlStateNormal];
        [rightButton setTitle:aTitle forState:UIControlStateNormal];
        if (aImage) {
            buttonWidth += aImage.size.width;
            buttonWidth += interval; // 文字和图片间距
            [rightButton setImage:aImage forState:UIControlStateNormal];
            [rightButton setImage:aImage forState:UIControlStateHighlighted];
            rightButton.imageEdgeInsets = UIEdgeInsetsMake(0, titleWidth+interval, 0, -(titleWidth+interval));
            rightButton.titleEdgeInsets = UIEdgeInsetsMake(0, -(aImage.size.width+interval), 0, aImage.size.width+interval);
        }
    } else if (aImage) {
        //扩大点击区域 图片靠右
        buttonWidth += aImage.size.width + 20;
        [rightButton setImage:aImage forState:UIControlStateNormal];
        [rightButton setImage:aImage forState:UIControlStateHighlighted];
        rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    }
    rightButton.frame = CGRectMake(0, 0, buttonWidth, 44);
    [rightButton addTarget:self
                    action:@selector(onNavigationRightButtonClicked:)
          forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    if (aTitle == nil && aImage == nil) [rightButton removeAllTargets];
    
}

#pragma mark - Event
// 左侧按钮点击
- (void)onNavigationLeftButtonClicked:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
    if (self.dismissAct) {
        self.dismissAct();
    }
}
// 右侧按钮点击
- (void)onNavigationRightButtonClicked:(UIButton *)sender {
    
}
// 中间按钮点击
- (void)onNavigationMiddleButtonClicked:(UISegmentedControl *)sender {
    
}
// 右侧第一个按钮点击
- (void)onNavigationRightButton1Clicked:(UIButton *)sender {
    
}
// 右侧第二个按钮点击
- (void)onNavigationRightButton2Clicked:(UIButton *)sender {
    
}

#pragma mark - associate object
- (void)setDismissAct:(void (^)(void))dismissAct {
    objc_setAssociatedObject(self, @selector(dismissAct), dismissAct, OBJC_ASSOCIATION_COPY);
}
- (void (^)(void))dismissAct {
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setLeftBarbuttonWidth:(CGFloat)leftBarbuttonWidth {
    objc_setAssociatedObject(self, @selector(leftBarbuttonWidth), @(leftBarbuttonWidth), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (CGFloat)leftBarbuttonWidth {
    return [objc_getAssociatedObject(self, _cmd) doubleValue];
}
- (void)setLotteryButton:(SEGActivityLotteryBubbleButton *)lotteryButton {
    objc_setAssociatedObject(self, @selector(lotteryButton), lotteryButton, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (SEGActivityLotteryBubbleButton *)lotteryButton {
    return objc_getAssociatedObject(self, _cmd);
}

@end
